// pages/bookpeople/bookpeople.js
import RegExp from "../../../utils/regular"
import http from "../../../utils/http"
Page({

    /**
     * 页面的初始数据
     */
    data: {
        username: "",//名字
        phone: "",//手机号码
        certificates: "",//证件号码
        allByPeopleList:[]
    },
    onLoad(option) {
        // 获取confirm传过来的值
        console.log(option);
        this.setData({
            username:option.name,
            phone:option.phone,
            certificates:option.certificateno,
        })
        this.id = option.id
        console.log(this.id);
        this.setData({
            tag:option.number
        })
    },
    // 验证手机号
    phoneBlur() {
        if (RegExp.regular(this.data.phone)) {
        } else {
       wx.showToast({
         title: '请输入正确的手机号',
         icon:"none"
       })
            return
        }
    },
    // 验证证件号码
    certificatesBlur() {
        if (RegExp.identity(this.data.certificates)) {
        } else {
            wx.showToast({
                title: '请输入正确的证件号',
                icon:"none"
              })
            return
        }
    },
    onShow() {
        this.getpuyTicketList()//购票人列表
    },
    //修改信息
  async  modify(){
      let userInfo = wx.getStorageSync('user')
        let { username, phone, certificates } = this.data
        let option ={
            url:"/scenicTicket/ticketBuyer/updateScenicTicketBuyer",
            data:{
                name: username,
                phone,
                certificateNo: certificates,
                userId: userInfo.user.id,
                buyerId:this.id,
                token: userInfo.token
            }
        }
        let res = await http(option)
        if(res.data.status == 200){
            wx.showToast({
              title: "修改成功",
            })
            setTimeout(()=>{
                wx.navigateBack({
                  delta: 1,
                })
            },1500)
        }else{
            wx.showToast({
                icon:'error',
                title: res.data.msg,
              })
        }
    },
    //获取购票人列表
    async getpuyTicketList(){
        let userDetail = wx.getStorageSync('user')
         let option = {
             url:"/scenicTicket/ticketBuyer/getScenicTicketBuyerList",
             data:{
                userId:userDetail.user.id,
                token: userDetail.token
             }
         }
         let res = await http(option)
         console.log(res);
         if(res.statusCode == 200){
             this.setData({
                allByPeopleList:res.data.data
             })
         }
    },
    // 保存信息
    async preservation() {
        let userDetail = wx.getStorageSync('user')
        let { username, phone, certificates } = this.data
        let ind = this.data.allByPeopleList.findIndex((item,index)=>{
            return item.name === username
        })
        if(ind != -1 && this.data.allByPeopleList.length > 0) {
            //说明购买人列表中已经有该用户
            wx.showToast({
              title: '用户已经存在',
              icon:"none"
            })
            return
        }else{
            if (RegExp.regular(phone) && phone != "" && RegExp.identity(certificates) && certificates != "") {
                let option = {
                    url: "/scenicTicket/ticketBuyer/saveScenicTicketBuyer",
                    data: {
                        name: username,
                        phone,
                        certificateNo: certificates,
                        userId:userDetail.user.id,
                        token:userDetail.token
                    }
                }
                let res = await http(option)
                console.log(res);
                if (res.data.status == 200) {
                    wx.showToast({
                        title: '添加成功',
                    })
                    setTimeout(() => {
                        wx.navigateBack({
                            delta: 1,
                        })
                    }, 1500)
                } else {
                    wx.showToast({
                        title:res.data.msg,
                        icon: "none"
                    })
                }
            }else{
                wx.showToast({
                  title: '请填写完整的的信息',
                  icon:"none"
                })
            }
        }
  
    },
    //返回上一个界面
    reture() {
        wx.navigateBack({
            delta: 1,
        })
    },
    // 点击右边了
    onClickRight() {
        wx.showToast({
            title: '点击右边了',
        })
        this.onClickIcon()
    }
})